﻿using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Database = Ledict.Core.Data.Database;

namespace Ledict.ConditionExtract.DAL
{
   public class SYS_CaseCauseElement_EvidRelDAL
    {
        private string DbName;

        public SYS_CaseCauseElement_EvidRelDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        public bool BatchAdd(IList<SYS_CaseCauseElement_EvidRel> list)
        {
            StringBuilder strSql = new StringBuilder();
            foreach (var Model in list)
            {              
                strSql.Append(string.Format(
                @" INSERT INTO [dbo].[SYS_CaseCauseElement_EvidRel]
           ([CER_ID]
           ,[CD_ID]
           ,[XmlPath]
           ,[UpdateTime]
           ,[EvidStatus])
     VALUES
           ('{0}','{1}','{2}','{3}','{4}'); ",
                Model.CER_ID, Model.CD_ID, Model.XmlPath, DateTime.Now, Model.EvidStatus));

            }

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            if (oBase.ExecuteNonQuery(CommandType.Text, strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddSYSCaseCauseElementEvidRel(SYS_CaseCauseElement_EvidRel Model)
        {
            string strSql = string.Format(
                @"INSERT INTO [dbo].[SYS_CaseCauseElement_EvidRel]
           ([CER_ID]
           ,[CD_ID]
           ,[XmlPath]
           ,[UpdateTime]
           ,[EvidStatus])
     VALUES
           ('{0}','{1}','{2}','{3}','{4}')",
                Model.CER_ID,Model.CD_ID,Model.XmlPath,Model.UpdateTime,Model.EvidStatus);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return Convert.ToInt32(oBase.ExecuteNonQuery(oCmd)) > 0 ? true : false;
        }

        /// <summary>
        /// 修改一条数据
        /// </summary>
        public bool UpdateSYSCaseCauseElementEvidRel(SYS_CaseCauseElement_EvidRel Model)
        {
            string strSql = string.Format(
                    @" update [dbo].[SYS_CaseCauseElement_EvidRel] set [CD_ID]='{0}',XmlPath='{1}',UpdateTime='{2}',EvidStatus='{3}' where CER_ID='{4}'",
                    Model.CD_ID,Model.XmlPath,DateTime.Now,Model.EvidStatus,Model.CER_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteSYSCaseCauseElementEvidRel(string CER_ID)
        {
           string strSql=string.Format(@" delete from SYS_CaseCauseElement_EvidRel where CER_ID='{0}' ", CER_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取要素证据表信息
        /// </summary>
        /// <param name="cdID"></param>
        /// <param name="ccName"></param>
        /// <returns></returns>
        public DataTable GetSysCaseCauseElementData(string cdID,string ccName)
        {
            string sql =string.Format(@" select * from[dbo].[SYS_CaseCauseElement_EvidRel] where CD_ID = '{0}' and CHARINDEX('/{1}', XmlPath)>0 ", cdID,ccName);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB != null && OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
        
    }
}
